package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 文件Controller
 *
 * @author cjie
 * @date 2021-06-30
 */
@Api(tags = "文件统一管理")
@RestController
@RequestMapping("/sysFile")
public class SysFileController extends BaseController {
    @Autowired
    private ISysFileService sysFileService;

    /**
     * 查询文件列表
     */
    @ApiOperation(value="查询文件列表", notes="查询文件列表")
    @PreAuthorize(hasPermi = "system:sysFile:list")
    @GetMapping("/list")
    public TableDataInfo list(SysFile sysFile) {
        startPage();
        List<SysFile> list = sysFileService.selectSysFileList(sysFile);
        return getDataTable(list);
    }

    /**
     * 导出文件列表
     */
    @ApiOperation(value="导出文件列表", notes="导出文件列表")
    @PreAuthorize(hasPermi = "system:sysFile:export")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFile sysFile) throws IOException {
        List<SysFile> list = sysFileService.selectSysFileList(sysFile);
        ExcelUtil<SysFile> util = new ExcelUtil<SysFile>(SysFile.class);
        util.exportExcel(response, list, "文件数据");
    }
    /**
     * 获取文件详细信息
     */
    @ApiOperation(value="获取文件详细信息", notes="获取文件详细信息")
    @PreAuthorize(hasPermi = "system:sysFile:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysFileService.selectSysFileById(id));
    }

    /**
     * 获取文件(内部调用)
     */
    @GetMapping(value = "/getOne/{id}")
    public R<SysFile> getOne(@PathVariable("id") Long id) {
        return R.ok(sysFileService.selectSysFileById(id));
    }

    /**
     * 通过linkId获取文件(内部调用)
     */
    @GetMapping(value = "/getList/{linkId}")
    public R<List<SysFile>> getList(@PathVariable("linkId") String linkId) {
        return R.ok(sysFileService.selectSysFileByLinkId(linkId));
    }

    /**
     * 新增文件(内部调用)
     */
    @ApiOperation(value="新增文件", notes="新增文件")
    @PreAuthorize(hasPermi = "system:sysFile:add")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFile sysFile) {
        return toAjax(sysFileService.insertSysFile(sysFile));
    }

    /**
     * 修改文件
     */
    @ApiOperation(value="修改文件", notes="修改文件")
    @PreAuthorize(hasPermi = "system:sysFile:edit")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFile sysFile) {
        return toAjax(sysFileService.updateSysFile(sysFile));
    }

    /**
     * 修改文件(内部调用)
     */
    @PostMapping("/update")
    public R update(@RequestBody SysFile sysFile) {
        return sysFileService.updateSysFile(sysFile)  > 0 ? R.ok() : R.fail();
    }

    /**
     * 删除文件
     */
    @ApiOperation(value="删除文件", notes="删除文件")
    @ApiImplicitParam(name = "ids", value = "文件ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "system:sysFile:remove")
    @Log(title = "文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysFileService.deleteSysFileByIds(ids));
    }

    /**
     * 通过linkId删除文件（内部调用)
     * @param linkId
     * @return
     */
    @DeleteMapping("/delete/{linkId}")
    public R delete(@PathVariable String linkId){
        return sysFileService.deleteSysFileByLinkId(linkId) > 0 ? R.ok() : R.fail();
    }

}
